# # 实例13.1
# import cv2
#
# img = cv2.imread("b2.jpg")
# gray =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(img,contours,-1,(0,0,255),5)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()
#

# 实例13.2
# import cv2
#
# img = cv2.imread("b2.jpg")
# cv2.imshow("img",img)
# img = cv2.medianBlur(img,5)
# gray =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# cv2.imshow("binary",binary)
#
# contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(img,contours,-1,(0,0,255),2)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()


# 13.3为爆炸图形绘制矩形包围框

# import cv2
# img=cv2.imread("c2.jpg")
# gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t,binary=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# contours,hierarchy=cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# x,y,w,h=cv2.boundingRect(contours[0])
# cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 13.4为爆炸图像绘制圆形包围框

import cv2

img = cv2.imread("c1.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
center,radius = cv2.minEnclosingCircle(contours[0])
x= int(round(center[0]))
y= int(round(center[1]))
cv2.circle(img,(x,y),int(radius),(0,0,255),2)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()

